// LucasLehmer, Performs the Lucas Lehmer algorithm
// Copyright (C) 2014  David McCloskey
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "common.h"
#include "options.h"
#include "execution.h"
#include "exceptions.h"

#include <iostream>
#include <tbb/task_scheduler_init.h>

void usage()
{
    std::cout << "lucaslehmer [options] <exponent>" << std::endl;
    std::cout << std::endl;
    std::cout << "<exponent> of the pattern \\d+ and > 0 and <= 4294967295" << std::endl;
    std::cout << std::endl;
    std::cout << "Options:" << std::endl;
    std::cout << "  -h, --help             Displays this screen" << std::endl;
}

inline bool verify_argument_uint32_t(const char * arg, uint32_t &value)
{
    uint32_t result = 0;

    size_t arg_length = strlen(arg);
    for(size_t i = 0; i < arg_length; ++i)
    {
        if(arg[i] < '0' || arg[i] > '9')
        {
            return false;
        }
        
        result = result * 10 + (arg[i] - '0');
    }
    
    value = result;
    return true;
}

bool parse_command_line(int argc, const char * argv[], options_t &options)
{
    try
    {
        for(int i = 1; i < (argc - 1); ++i)
        {
            size_t argLength = strlen(argv[i]);
            if(argLength == 2 && argv[i][0] == '-')
            {
                switch(argv[i][1])
                {
                    case 'h':
                        usage();
                        return false;
                        
                    default: throw unknown_argument(argv[i]);
                }
            }
            else if(argLength > 2 && argv[i][0] == '-' && argv[i][1] == '-')
            {
                if(0 == strcasecmp(argv[i], "--help"))
                {
                    usage();
                    return false;
                }
                else
                {
                    throw unknown_argument(argv[i]);
                }
            }
            else
            {
				throw unknown_argument(argv[i]);
            }
        }

		// Verify the exponent
		if (!verify_argument_uint32_t(argv[argc - 1], options.exponent) || options.exponent == 0)
		{
			throw invalid_exponent(argv[argc - 1]);
		}

        return true;
    }
    catch(const unknown_argument &e)
    {
        usage();
        std::cout << "ERROR: Unknown argument" << e.what() << std::endl;
    }
    
    return false;
}

int main(int argc, const char * argv[])
{
    try
    {
		// Parse the command-line, building the options
		options_t options;
		options.algorithm = ALG_BASIC_MERSENNE;
		options.threads = tbb::task_scheduler_init::default_num_threads();

        if(parse_command_line(argc, argv, options))
        {
			if (execute(options))
			{
				std::cout << "Status Report: Exponent " << options.exponent << " passed" << std::endl;
			}
			else
			{
				std::cout << "Status Report: Exponent " << options.exponent << " failed" << std::endl;
			}
        }
    }
    catch(const std::exception &e)
    {
        std::cout << e.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "EXCEPTION: Unknown" << std::endl;
    }
    
    return 0;
}

